﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading; 

namespace MathQ
{
    public partial class MathEquaCtrl : UserControl
    {
        private MathEqua _MathEqua;

        public MathEquaCtrl(MathEqua mEq)
        {
            InitializeComponent();
            this._MathEqua = mEq;
        }

        private void MathEqua_Load(object sender, EventArgs e)
        {
            pictureBox_result.SizeMode = PictureBoxSizeMode.StretchImage;
            comboBox_answ.Enabled = false;
            InitEquation();
        }


        public void EnableAnswCombo()
        {
            comboBox_answ.Enabled = true;
        }


        public void DisableAnswCombo()
        {
            comboBox_answ.Enabled = false;
        }


        public void InitEquation()
        {
            label_x.Text = _MathEqua.X.ToString();
            label_y.Text = _MathEqua.Y.ToString();
            label_optr.Text = _MathEqua.OptTxt;

            if (comboBox_answ.Items.Count < 1)
            {
                for (int i = 0; i <= 40; i++)
                {
                    comboBox_answ.Items.Add(i);
                }
            }
            comboBox_answ.Text = "";

            if (pictureBox_result.Visible) pictureBox_result.Visible = false;
        }


        public bool CheckResult()
        {
            bool isCorrect = false;
            int? answ = null;
            try
            {
                answ = Convert.ToInt32(comboBox_answ.Text);
            }
            catch
            {
                //MessageBox.Show("请输入有效的数字！", "请确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (answ != null)
            {
                isCorrect = (_MathEqua.Answer == (int)answ);

                Thread.Sleep(50);
                pictureBox_result.Visible = true;
                if (isCorrect)
                {
                    pictureBox_result.Image = Program._correctImages[Program._rnd.Next(0, 3)];
                    pictureBox_result.Refresh();
                }
                else
                {
                    pictureBox_result.Image = Program._wrongImages[Program._rnd.Next(0, 2)];
                    pictureBox_result.Refresh();
                }
            }
            return isCorrect;
        }


        public bool HasValue
        {
            get { return (comboBox_answ.Text.Length > 0); }
        }

        public MathEqua MathEqua
        {
            get { return _MathEqua; }
            set 
            { 
                _MathEqua = value;
                InitEquation();
            }
        }


        private void comboBox_answ_Leave(object sender, EventArgs e)
        {
            if (comboBox_answ.Text.Length > 0)
            {
                try
                {
                    int answ = Convert.ToInt32(comboBox_answ.Text);
                }
                catch
                {
                    MessageBox.Show("请输入有效的数字！", "请确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    comboBox_answ.Focus();
                }
            }
        }


        public bool IsEnabled
        {
            get { return comboBox_answ.Enabled; }
        }
    }
}
